package com.lala.controller;

import com.lala.entity.ClientLogin;
import com.lala.entity.ClientMessage;
import com.lala.exception.LoginException;
import com.lala.service.ClientLoginService;
import com.lala.service.ClientMessageService;
import com.lala.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/user")

public class UserController {
@Autowired
    ClientLoginService clientLoginService;
@Autowired
    RedisTemplate redisTemplate;
@Autowired
    ClientMessageService messageService;
  /*  @GetMapping("/seachname")
    @ResponseBody
    public ResultJson seachname(String username){
        return new ResultJson(200,"ok",null);
    }*/

    /**
     * @author 霍明铭
     * @param clientLogin
     * @param tel
     * @param mail
     * @return  注册用户
     */
   @PostMapping("/register")
   @ResponseBody
   public ResultJson index(ClientLogin clientLogin,Integer tel,String mail,String code){
      if (!code.equals(redisTemplate.opsForValue().get("code"))){
          return   new ResultJson(500,"验证码错误",null);
      }
       ClientLogin clientLogin1= clientLoginService.seachexit(clientLogin.getUserName());
       if (clientLogin1!=null){
           return new ResultJson(500,"此用户名已存在",null);
       }
       try {
           clientLoginService.save(clientLogin,tel,mail);
           return  new ResultJson(200,"添加成功",null);
       }catch (Exception e){
           return new ResultJson(200,"添加失败",null);
       }
   }
   //登录用户
   @PostMapping("/login")
   @ResponseBody
    public  ResultJson login(ClientLogin clientLogin, String safecode){
       if (!safecode.equals(redisTemplate.opsForValue().get("sofecode"))){
           return new ResultJson(500,"验证码不正确",null);
       }
      try {
         ClientLogin login= clientLoginService.getCLByNameAndPwd(clientLogin);
         redisTemplate.opsForValue().set("user",login);
         redisTemplate.expire("user",30, TimeUnit.MINUTES);
          return  new ResultJson(200,"登录成功",login);
      }catch (Exception e){
          return new ResultJson(500,e.getMessage(),null);
      }
   }
   //通过邮箱找寻密码
    @PostMapping("/pwdfind")
    @ResponseBody
    public  ResultJson pwdfind(String mail,String newPassword,String mailcode ) {
      if (!mailcode.equals(redisTemplate.opsForValue().get("mailcode"))){
          return new ResultJson(500,"验证码错误",null);
      }
   try {
       clientLoginService.getPassWordAndPwd(mail,newPassword);
       return new ResultJson(200,"成功",null);
   }catch (LoginException e){
       return new ResultJson(500,e.getMessage(),null);
   } catch (Exception e){
       return new ResultJson(500,"失败",null);
   }
    }
    //退出用户
    @GetMapping("/out")
    @ResponseBody
    public ResultJson out(){
       redisTemplate.delete("user");
       return new ResultJson(200,"成功",null);
    }
    //查询个人信息
    @GetMapping("/message")
    @ResponseBody
    public ResultJson message(Integer loginId){
     try {
         ClientMessage message= messageService.getMg(loginId);
         System.out.println(message);
         return  new ResultJson(200,"信息",message);
     }catch (Exception e){
         return  new ResultJson(500,"信息",null);
     }

    }
    @PutMapping("/updatemessage")
    @ResponseBody
    public  ResultJson updateMessage(ClientMessage clientMessage){
        System.out.println(clientMessage);
        try {
            messageService.updateMessage(clientMessage);
            return new ResultJson(200,"成功",null);
        }catch (Exception e){
            return new ResultJson(500,"失败",null);
        }

    }


}
